package com.linxiao.hrcrm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.linxiao.hrcrm.domain.CrmProject;
import com.linxiao.hrcrm.service.ICrmProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目信息Controller
 *
 * @author zhoudong
 * @date 2024-01-03
 */
@RestController
@RequestMapping("/hrcrm/project")
public class CrmProjectController extends BaseController {
    @Autowired
    private ICrmProjectService crmProjectService;

    /**
     * 查询项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmProject crmProject) {
        startPage();
        List<CrmProject> list = crmProjectService.selectCrmProjectList(crmProject);
        return getDataTable(list);
    }

    /**
     * 查询项目信息列表（不分页）
     */
    @GetMapping("/all")
    public TableDataInfo all(CrmProject crmProject) {
        List<CrmProject> list = crmProjectService.selectCrmProjectList(crmProject);
        return getDataTable(list);
    }

    /**
     * 导出项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:project:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmProject crmProject) {
        List<CrmProject> list = crmProjectService.selectCrmProjectList(crmProject);
        ExcelUtil<CrmProject> util = new ExcelUtil<CrmProject>(CrmProject.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    /**
     * 获取项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(crmProjectService.selectCrmProjectById(id));
    }

    /**
     * 新增项目信息
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:project:add')")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmProject crmProject) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        crmProject.setCreateBy(loginUser.getUser().getNickName());
        return toAjax(crmProjectService.insertCrmProject(crmProject));
    }

    /**
     * 修改项目信息
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:project:edit')")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmProject crmProject) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        crmProject.setUpdateBy(loginUser.getUser().getNickName());
        return toAjax(crmProjectService.updateCrmProject(crmProject));
    }

    /**
     * 删除项目信息
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:project:remove')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(crmProjectService.deleteCrmProjectByIds(ids));
    }
}
